<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\common\validate;


use app\api\service\Token;
use app\common\exception\ExceptionEmun;
use app\common\exception\MenuException;
use app\common\model\MemberMenu;
use app\common\traits\InstanceTrait;
use app\common\validate\BaseValidate;
use app\lib\exception\TokenException;

/**
 * Class MenuValidate
 */
class MemberMenuValidate extends BaseValidate
{
    use InstanceTrait;

    protected $rule = [
        'id|ID' => ['require',],
        'ids|主键数组' => ['require',],
        'status|状态' => ['require',],
        'title|标题' => ['require',],
        'pid|父权限' => ['require',],
        'module|模块' => ['require',],
    ];

    protected $sceneRule = [
        'index' => [
            'only' => [''],
        ],
        'read' => [
            'only' => ['id'],
        ],
        'delete' => [
            'only' => ['id'],
        ],
        'deletes' => [
            'only' => ['ids'],
        ],
        'enables' => [
            'only' => ['ids', 'status'],
        ],
        'save' => [
            'only' => ['title', 'pid', 'module'],
            'append' => [
                'pid' => 'checkPidRight',
                'title' => 'checkNameUnique',
            ],
        ],
        'update' => [
            'only' => ['title', 'pid', 'module'],
            'append' => [
                'pid' => 'checkPidRight',
                'title' => 'checkNameUnique',
            ],
        ],
    ];
    /**
     * 检测父ID是否存在
     * @param $value
     * @return bool
     * @throws \Exception
     */
    protected function checkPidRight($value, $rule, $data = [])
    {
        //pid可以是零
        if (!empty($value)) {
            $AdminRule = MemberMenu::find($value);
            if (empty($AdminRule)) {
                return ExceptionEmun::throwException(MenuException::PID_NO_EXIST);
            }
            if (!empty($data['module'])) {
                if ($data['module'] !== $AdminRule['module']) {
                    return ExceptionEmun::throwException(MenuException::PID_NO_RIGHT);
                }
            }
        }
        return true;
    }

    /**
     * 检测菜单名重复
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    protected function checkNameUnique($value, $rule, $data = [])
    {
        $MemberMenu = MemberMenu::instance();
        $adminMenu = $MemberMenu->where('title', '=', $value)
                               ->where('module', '=', $data['module'])
                               ->find();
        $pk = 'id';
        if (!empty($adminMenu)) {
            if (!empty($data[$pk])) {
                //修改帐号
                if ($data[$pk] != $adminMenu['menu_id']) {
                    return ExceptionEmun::throwException(MenuException::NAME_REPEAT);
                }
            } else {
//                创建帐号
                return ExceptionEmun::throwException(MenuException::NAME_REPEAT);
            }
        }
        return true;
    }
}
